package com.rh.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rh.dto.Page;
import com.rh.dto.Result;
import com.rh.entity.po.User;
import com.rh.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    //展示列表
    @RequestMapping("/getUserPage")
    public Result getUserPage(Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=2;
        }
        Result result = new Result();
        Page userPage = userService.getUserPage(pageNo, pageSize);
        result.setData(userPage);
        return result;
    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @RequestMapping("/deleteBatch")
    public Result deleteBatch(String ids) {
        Result result = new Result();
        List<Integer>doList=new ArrayList<>();
        String[] strs=ids.split(",");
        for (String str : strs) {
            doList.add(Integer.valueOf(str));

        }
        userService.deleteBatchUser(doList);
        return result;
    }

    /**
     * 添加
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    public Result addUser(User user){
        userService.addUser(user);
        Result result = new Result();
        return result;
    }

    /**
     * 删除
     * @param userId
     * @return
     */
    @RequestMapping("/deleUser")
    public Result deleUser(Integer userId){
        userService.deleteUser(userId);
        Result result = new Result();
        return result;
    }
    /**
     * 模糊查询
     */
    @RequestMapping("/selectUserByName")
    public Result selectUserByName(String userName){
        Result result = new Result();
        List<User> users = userService.queryUserByName(userName);
        result.setData(users);
        return result;
    }
}
